package com.hyts.codegen.process.writer.item;

import com.hyts.codegen.common.config.JavaWriterConfig;
import com.hyts.codegen.common.handle.JdbcMetaDataLoader;
import com.hyts.codegen.common.model.JavaDocModel;
import com.hyts.codegen.common.model.JavaFieldModel;
import com.hyts.codegen.common.model.JdbcGenModel;
import com.hyts.codegen.common.writer.JavaCodeWriter;
import com.hyts.codegen.process.bean.JavaBeanModel;
import com.hyts.codegen.process.writer.JavaProcessStringWriter;
import com.hyts.dbmeta.model.TableMetadata;
import com.hyts.guava.String.Strings;
import com.hyts.scanner.utility.PathUtils;
import com.hyts.template.common.BaseJavaFileTemplate;
import com.hyts.template.strformat.StringCodeTemplate;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.codegen.process.writer.item
 * author:Libo/Alex
 * create-date:2019-06-26 14:37
 * email:liboware@gmail.com
 * description:此类主要用于: 代码程序字符串写入器
 */
@Slf4j
public class JavaCodeProcessStringWriter extends JavaProcessStringWriter<JavaBeanModel> {

    /**
     * 对象操作服务
     */
    List<TableMetadata> tableMetadata;

    /**
     * jdbc生成数据对象
     */
    JdbcGenModel jdbcGenModel;


    /**
     * 构造器对象
     */
    public JavaCodeProcessStringWriter(boolean load){
        if(load) {
            JdbcMetaDataLoader jdbcMetaDataLoader = JdbcMetaDataLoader.builder().build();
            tableMetadata = jdbcMetaDataLoader.load();
            jdbcGenModel = jdbcMetaDataLoader.getJdbcGenModel();
        }
    }

    /**
     * 构造器对象
     */
    public JavaCodeProcessStringWriter(){
        this(true);
    }
    /*tempate.genImportContent(new String[]{
                "import javax.persistence.Column;",
                "import javax.persistence.Entity;",
                "import javax.persistence.GeneratedValue;",
                "import javax.persistence.Id;",
                "import javax.persistence.Table;"
        });*/
    @Override
    public String buildModel(JavaBeanModel model) {
        //基础操作template模板功能对象
        BaseJavaFileTemplate tempate = new StringCodeTemplate();
        //构建package语句的相关操作
        tempate.genPackageContent(model.getBasePackageName(),model.getModelPackageName());
        //构建import语句操作服务
        tempate.genImportContent(model.getImportClasses());
        //构建class file文件注释 javadoc
        tempate.genJavaDocContent(model.getDocModel().getAnnotationMap(),"    ");
        //构建class 类名以及访问权限标识
        tempate.genTypeContent(model.getTypeModel().getJavaType(), model.getTypeModel().getAccessType(),
                model.getTypeModel().getClassName()+model.getBeanType().name(), model.getTypeModel().getAnnotations());
        //构建field字段服务-因为采用了lombok配置
        model.getFieldModels().parallelStream().forEach(param->{
            tempate.genFieldContent(param.getAccessType(), param.getReturnType(), param.getFieldName(), "    ", null);
        });
        // 构建fullname名称
        String fullName = model.getBasePackageName()+"."+model.getBasePackageName()+model.getTypeModel().getClassName();
        return JavaCodeWriter.builder().build().writerInternal
                (PathUtils.package2Path2(fullName)+ JavaWriterConfig.JAVA_SUBFIX_NAME,tempate.end().toString());
    }

    @Override
    public String buildDAO(JavaBeanModel model) {
        return null;
    }

    @Override
    public String buildService(JavaBeanModel model) {
        return null;
    }

    @Override
    public String buildController(JavaBeanModel model) {
        return null;
    }


    /**
     * 转换model信息服务
     * @param model
     * @return
     */
    public JavaBeanModel.BeanType transferModel(JavaBeanModel model){
        model.setBeanType(model.getBeanType() == null? JavaBeanModel.BeanType.PO:model.getBeanType());
        return model.getBeanType();
    }




    public static void main(String[] args){
        JavaBeanModel model = new JavaBeanModel();
        new JavaCodeProcessStringWriter().init();
    }


    @Override
    public void init() {
        log.info("开始初始化的相关对象构建生成类");
        String basePackageName = jdbcGenModel.getGenModel().getPackageName()[0];
        Map<String,String> annotationMap = new HashMap<>();
        annotationMap.put("author","alex/libo");
        annotationMap.put("date","20180509");
        annotationMap.put("description","coffice-codegen generator");
        tableMetadata.parallelStream().forEach(param->{
            JavaBeanModel javaBeanModel = new JavaBeanModel();
            javaBeanModel.setBasePackageName(basePackageName);
            javaBeanModel.setModelPackageName(Strings.underLineUpperCaseToUpperCamel(param.getName()));
            javaBeanModel.setBeanType(JavaBeanModel.BeanType.PO);
            javaBeanModel.setDocModel(new JavaDocModel(annotationMap));
            List<JavaFieldModel> fieldModels = new LinkedList<>();
            javaBeanModel.setFieldModels(getFieldModelList(param));
            buildModel(javaBeanModel);
        });
        log.info("结束初始化的相关对象构建生成类");
    }

    /**
     * field字段modellist
     * @param tableMetadata
     * @return
     */
    List<JavaFieldModel> getFieldModelList(TableMetadata tableMetadata){
        return tableMetadata.getColumnMetadataList().parallelStream().map(param->{
            JavaFieldModel javaFieldModel = new JavaFieldModel();
            javaFieldModel.setHeadSpace("    ");
            javaFieldModel.setReturnType(param.getJdbcType());
            javaFieldModel.setFieldName(Strings.underLineUpperCaseToLowerCamel(param.getColumnName()));
//            javaFieldModel.setAnnotations();
            javaFieldModel.setAccessType("private");
            return javaFieldModel;
        }).collect(Collectors.toList());
    }


}
